package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.AddressDao;
import cn.gjculture.shopping.api.dao.CommonDeleteMapper;
import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.entity.Address;
import cn.gjculture.shopping.api.service.AddressService;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressDao addressDao;

    @Autowired
    CommonDeleteMapper commonDao;

    @Autowired
    CommonUpdateMapper commonUpdateDao;

    @Override
    public int insert(Address address) {
        address.setAddressId(UUIDUtil.getUUID());
        if(addressDao.count(address.getCustomerId())==0){
            address.setStatus(8);
        }else if(address.getStatus()!=8){
            address.setStatus(0);
        }else if(address.getStatus()==8){
            Address a=new Address();
            a.setCustomerId(address.getCustomerId());
            a.setStatus(0);
            addressDao.updateStatusByCustomer(address);
        }
        return addressDao.insert(address);
    }

    @Override
    public List<Address> query() {
        List<Address> activities=addressDao.query();
        return activities;
    }

    @Override
    public Address queryById(String addressId) {
        Address activity=addressDao.queryById(addressId);
        return activity;
    }


    @Override
    public int deleteMore(List<String> addressIds) {
        return commonDao.deleteAddress(addressIds);
    }

    @Override
    public int update(Address address) {
        return commonUpdateDao.updateAddress(address);
    }

    @Override
    public List<Address> queryByCustomerId(String customerId) {
        List<Address> addresses=addressDao.queryByCustomerId(customerId);
        return addresses;
    }

    @Override
    public int setDefault(String addressId) {
        Address address=addressDao.queryById(addressId);
        address.setStatus(0);
        addressDao.updateStatusByCustomer(address);
        address.setAddressId(addressId);
        address.setStatus(8);
        addressDao.updateStatusById(address);
        return 0;
    }
}
